<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/20 0020
 * Time: 17:02
 */

namespace api\controllers;


use api\components\BaseApiComponentController;
use common\components\DataHelper;
use common\components\HttpClient;
use common\components\Services\Wechat;
use common\models\PlugsRunTotal\PlugsRunTotal;
use common\models\QrcodeLog\QrcodeLog;
use common\models\WxDisabledArea\WxDisabledArea;
use common\models\WxPublic\WxPublic;
use yii\helpers\ArrayHelper;

class WxPublicController extends BaseApiComponentController
{
    public function actionQr()
    {
        //获取客户端IP
        $ip = /*"113.251.17.153";#*/DataHelper::getIp();
        $httpClient = new HttpClient();
        $area = $httpClient->sendRequest("http://api.map.baidu.com/location/ip", "get", [
            "ip" => $ip,
            "ak" => DataHelper::getConfig("BMap_key")
        ]);
        $city = ArrayHelper::getValue($area, "content.address_detail.city", false);
        $city =  PlugsRunTotal::formatAddress($city);
        if ( !array_filter($city) ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, "未知的区域");
        }

        $disabledPublicId = WxDisabledArea::find()->where([
            "city" => $city
        ])->select("wx_public_id")->asArray()->all();
        $disabledPublicId = $disabledPublicId ? array_column($disabledPublicId, "wx_public_id") : [];
        $wxPublic = WxPublic::find()->where(WxPublic::getExposureNumCondition())->orderBy("exposure_num desc");
        if ( $disabledPublicId ) {
            $wxPublic->andWhere([
                "or",[
                    "in",
                    "id",
                    $disabledPublicId
                ],[
                    "=", "is_all_area", WxPublic::IS_YES
                ]
            ]);
        } else {
            $wxPublic->andWhere([
                "is_all_area" => WxPublic::IS_YES
            ]);
        }
        $wxPublic = $wxPublic->one();
        if ( !$wxPublic ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, "没有可用的公众号");
        }
        $wechat = Wechat::NewWechat($wxPublic->app_id, $wxPublic->secret);
        $ret = $wechat->generateTemporaryQrCodeForNil(QrcodeLog::TYPE_WX_PUBLIC, [
            QrcodeLog::EXTENDS_JSON_KEY_VERIFY_CODE => DataHelper::random(4),
            QrcodeLog::EXTENDS_JSON_KEY_WX_PUBLIC_ID => $wxPublic->id
        ],$ip);
        if ( !$ret["status"] ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, $ret["message"]);
        }
        //增加曝光量
        $wxPublic->addTotalExposure();
        return $this->jsonResponse(self::RESPONSE_SUCCESS, $ret);
    }

    public function actionCheckCode()
    {
        $qrCodeLog = QrcodeLog::findOne([
            "id" => $this->get("qr_id"),
            "type" => QrcodeLog::TYPE_WX_PUBLIC
        ]);

        if ( !$qrCodeLog ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, "验证码已失效");
        }
        if ( !$qrCodeLog->isScan() ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, "未扫码");
        }
        $code = $qrCodeLog->getExtendJson(QrcodeLog::EXTENDS_JSON_KEY_VERIFY_CODE);
        if ( !$code ) {
            return $this->jsonResponse(self::RESPONSE_FAILURE, "验证码已失效");
        }
        if ( $code == $this->get("code") ) {
            $qrCodeLog->delete();
            return $this->jsonResponse(self::RESPONSE_SUCCESS, "操作成功");
        }
        return $this->jsonResponse(self::RESPONSE_FAILURE, "验证码错误");
    }
}